package com.codes.controller;

import com.codes.core.common.CConstant;
import com.codes.core.common.enumtype.RoleType;
import com.codes.core.common.result.Result;
import com.codes.core.utils.NubmerUtil;
import com.codes.model.Logistics;
import com.codes.service.LogisticsSV;
import com.codes.service.UserSV;
import com.codes.service.WorkOrderSV;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.collections4.ResettableListIterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2020/5/29 9:30
 * @Created by peanut
 */

@Api(tags = "物流接口")
@RestController
@RequestMapping("/logistics")
@Slf4j
public class LogisticsCtr {

    @Autowired
    private UserSV userSV;
    @Autowired
    private LogisticsSV logisticsSV;
    @Autowired
    private WorkOrderSV workOrderSV;


    @ApiOperation("待运输  和 已运输列表 wx")
    @PostMapping("/getlist_page")
    @DynamicParameters(name = "CreateOrderMapModel", properties = {
            @DynamicParameter(name = "driverId", value = "1"),
            @DynamicParameter(name = "isStart", value = "0待运输1正在运输"),
    })
    public Result getListPage(@RequestParam(defaultValue = "1", required = true) Integer page,
                              @RequestParam(defaultValue = "10", required = true) Integer limit,
                              @RequestBody Map<String, Object> map) {

          page = MapUtils.getInteger(map,"page");
          limit = MapUtils.getInteger(map,"limit");

        PageHelper.startPage(page, limit);
        // Map<String, Object> map =  new HashMap<>();
        List<Map<String, Object>> users = logisticsSV.getListByDriverId(map);
        PageInfo<Map<String, Object>> info = new PageInfo<>(users);
        return Result.pages(info.getList(), (int) info.getTotal());
    }
    @ApiOperation("待运输  和 已运输列表 web")
    @GetMapping("/page")
    public Result getPage(@RequestParam(defaultValue = "1", required = true) Integer page,
                              @RequestParam(defaultValue = "10", required = true) Integer limit,
                              @RequestParam Map<String, Object> map) {

        PageHelper.startPage(page, limit);
        // Map<String, Object> map =  new HashMap<>();
        List<Map<String, Object>> users = logisticsSV.getListByDriverId(map);
        PageInfo<Map<String, Object>> info = new PageInfo<>(users);
        return Result.pages(info.getList(), (int) info.getTotal());
    }


    @ApiOperation("获取司机物流单详情")
    @GetMapping("/get_info")
    public Result getInfo(@RequestParam Map<String, Object> map) {

        Integer logisticsId = MapUtils.getInteger(map, "logisticsId");
        if (null != logisticsId) {
            map = logisticsSV.getInfoByLogisticsId(map);
            return Result.success(map);
        }
        return Result.failed();
    }

    @ApiOperation("分配任务")
    @PostMapping("/allocate")
    public Result allocate(@RequestBody Map<String, Object> map) {

        log.info("分配物流");
        int re = logisticsSV.insertLogisticeInfo(map);
        if (re == 0) {
            return Result.failed("物流分配失败");
        }
        return Result.success("物流分配成功", "ok");
    }

    // delivery  交付

    /**
     * 参数  工单id  物流单 id   送达时间, 提交状态, 完成与否的原因
     *
     * @return
     */
    @ApiOperation("司机交付")
    @PostMapping("/delivery")
    @Transactional
    public Result delivery(@RequestBody Map<String, Object> map) {
        //  交付流程   司机扫码 扫 客户的 工单ID生成的二维码 , 对比当前的 司机 物流单id   一致 则 可以交付...
        // 修改 物流单 状态为 正常送达, 延迟送达, 未完成. 及原因  送达时间
        int re = 0;
        re = logisticsSV.updateByMap(map);
        // 完成度  如 三个司机单,   已完成 1个  现在交付 一个.
        Integer workOrderId = MapUtils.getInteger(map, "workOrderId");
        if (null != workOrderId) {
            // 计算需求工单完成度
            map.put("finishState", null);// 清空状态 查询全部
            int total = logisticsSV.getCountByOrderId(map);
            map.put("finishState", 12);// 查询 状态为 1 2 的物流单 表示为 已完成
            int finish = logisticsSV.getCountByOrderId(map);
            // 计算 百分比
            String performance = NubmerUtil.baifenbi(finish, total);
            map.put("performance", performance);
            map.put("finishState", 0);
            int noFinish = logisticsSV.getCountByOrderId(map);  // 如果 没完成的 数量为0 表示 工单完结
            if (0 == noFinish) {
                map.put("state", CConstant.WORKORDER_STATE_END);// 工单完结
            }else {
                map.put("state", CConstant.WORKORDER_STATE_POST);// 工单配送
            }
            //修改 工单完成度  及 状态
            re = workOrderSV.updateByPrimaryKeySelective(map);
            if (re > 0) {
                return Result.success();
            }
        }

        return Result.failed();
    }

    @ApiOperation("删除物流工单")
    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id){

        logisticsSV.deleteByPrimaryKey(id);
        return  Result.success("删除成功");
    }


    //end
}